package com.flight.trans.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.activity.LeavePortCollectCheckDetailsActivity;
import com.flight.trans.adapter.LeavePortCollectCheckAdapter;
import com.flight.trans.bean.LeavePortCollectCheckBean.LeavePortCollectCheckData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.util.ChinaToPinyin;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ProgressUtils;
import com.flight.trans.view.HeaderTitleView;
import com.flight.trans.base.BaseFragment;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 出港核查
 */
public class LeavePortCollectCheckFragment extends BaseFragment {

    private HeaderTitleView mHeaderView ;

    private TextView mTvOrderNo ,//单号
            mTvOrderCount ,//运单件数
            mTvProductName ,//品名
            mTvDeep ,//目的地
            mTvFlightNo ,//航班号
            mTvSender ;//发货人

    private TwinklingRefreshLayout mRefreshLayout ;
    private RecyclerView mRvContainer ;
    private RelativeLayout mRlContentNull ;

    private LeavePortCollectCheckAdapter mCollectCheckAdapter;
    private List<LeavePortCollectCheckData> mCollectCheckDataList ,mSearchDataList;

    private int mSortState = -1;

    @Override
    public int setContentView() {
        return R.layout.fragment_leave_port_collect_check;
    }

    @Override
    public void initView() {
        mHeaderView = mView.findViewById(R.id.mHeaderView);
        mRefreshLayout = mView.findViewById(R.id.mRefreshLayout);
        mRvContainer = mView.findViewById(R.id.mRvContainer);
        mRlContentNull = mView.findViewById(R.id.mRlContentNull);

        mTvOrderNo = mView.findViewById(R.id.mTvOrderNo);
        mTvOrderCount = mView.findViewById(R.id.mTvOrderCount);
        mTvProductName = mView.findViewById(R.id.mTvProductName);
        mTvDeep = mView.findViewById(R.id.mTvDeep);
        mTvFlightNo = mView.findViewById(R.id.mTvFlightNo);
        mTvSender = mView.findViewById(R.id.mTvSender);
    }

    @Override
    public void initData() {
        initHeaderView();

        mTvOrderNo.setOnClickListener(this::onClickSort);
        mTvOrderCount.setOnClickListener(this::onClickSort);
        mTvProductName.setOnClickListener(this::onClickSort);
        mTvDeep.setOnClickListener(this::onClickSort);
        mTvFlightNo.setOnClickListener(this::onClickSort);
        mTvSender.setOnClickListener(this::onClickSort);

        CommonUtils.setScreenViewVisible(mTvDeep);
        CommonUtils.setScreenViewVisible(mTvFlightNo);
        CommonUtils.setScreenViewVisible(mTvSender);

        setRefreshListener(mRefreshLayout);
    }

    @Override
    public void initConnect() {
        if(mHeaderView == null) return;

        valueParams = new HttpParams();
        valueParams.put(Constant.Key_State , mHeaderView.getSelectCheckState());
        if(mHeaderView.getSelectCheckState() != 0)
            valueParams.put(Constant.Key_Order_Create_Date , mHeaderView.getSelectDate());

        NetworkUtils.getLeavePortCollectCheckList(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mCollectCheckDataList = (List<LeavePortCollectCheckData>) body;

                onScanResult(mHeaderView.getSearchContent());
            }
        }, valueParams);
    }

    private void initAdapter(){
        mRlContentNull.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.GONE : View.VISIBLE);

        if(mCollectCheckAdapter == null) {
            mCollectCheckAdapter = new LeavePortCollectCheckAdapter(mActivity, mSearchDataList);
            mRvContainer.setAdapter(mCollectCheckAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mCollectCheckAdapter.setOnItemClickListener(new CollectCheckItemClickListener());
        }else {
            mCollectCheckAdapter.updateAdapter(mSearchDataList);
        }
    }

    /**
     * 条目点击事件
     */
    private class CollectCheckItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            NetworkUtils.checkPageLock(new OkGoBackListener(mActivity) {
                @Override
                public void onSuccess(Object body) {
                    mIntent = new Intent(mActivity , LeavePortCollectCheckDetailsActivity.class);
                    mBundle = new Bundle();
                    mBundle.putSerializable(Constant.INFO , mSearchDataList.get(position));
                    mBundle.putInt(Constant.SelectState , mHeaderView.getSelectCheckState());
                    mIntent.putExtras(mBundle);
                    turnActForResult(mIntent);
                }
            }, ConstantInfo.OUT_STORAGE_PRE_CHECK , mSearchDataList.get(position).getBizOrderId());
        }
    }

    private void initHeaderView() {
        mHeaderView.addOnHeaderListener(new HeaderTitleView.OnHeaderListener(){
            @Override
            public void onSearch(String search) {
                onScanResult(search);
            }

            @Override
            public void onSwitchChange(int type, int index) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }

            @Override
            public void onDateChange(String date) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }
        });
    }

    @Override
    public void onScanResult(String code) {
        mHeaderView.setSearchContent(code);

        mSearchDataList = new ArrayList<>();
        if(!TextUtils.isEmpty(code)) {

            for (int i = 0; i < mCollectCheckDataList.size(); i++)
                if(mCollectCheckDataList.get(i).getOrderNo() != null && mCollectCheckDataList.get(i).getOrderNo().contains(code))
                    mSearchDataList.add(mCollectCheckDataList.get(i));
        }else {
            mSearchDataList.addAll(mCollectCheckDataList);
        }


        if(mSearchDataList != null)
            if(mSortState == 0){
                Collections.reverse(mSearchDataList);
            }else if(mSortState != -1){
                Collections.sort(mSearchDataList, new SortComparator(mSortState));
            }

        initAdapter();
    }

    public void onClickSort(View view){
        mSortState = (mSortState != 0 && R.id.mTvOrderNo == view.getId()) ? 0 : (mSortState == 0 ? -1 : mSortState) ;
        mSortState = (mSortState != 1 && R.id.mTvOrderCount == view.getId()) ? 1 : mSortState == 1 ? -1 : mSortState ;
        mSortState = (mSortState != 2 && R.id.mTvProductName == view.getId()) ? 2 : mSortState == 2 ? -1 : mSortState ;
        mSortState = (mSortState != 3 && R.id.mTvDeep == view.getId()) ? 3 : mSortState == 3 ? -1 : mSortState ;
        mSortState = (mSortState != 4 && R.id.mTvFlightNo == view.getId()) ? 4 : mSortState == 4 ? -1 : mSortState ;
        mSortState = (mSortState != 5 && R.id.mTvSender == view.getId()) ? 5 : mSortState == 5 ? -1 : mSortState ;

        mTvOrderNo.setTextColor(mActivity.getColor(mSortState == 0 ? R.color.color_red : R.color.color_blue));
        mTvOrderCount.setTextColor(mActivity.getColor(mSortState == 1 ? R.color.color_red : R.color.color_blue));
        mTvProductName.setTextColor(mActivity.getColor(mSortState == 2 ? R.color.color_red : R.color.color_blue));
        mTvDeep.setTextColor(mActivity.getColor(mSortState == 3 ? R.color.color_red : R.color.color_blue));
        mTvFlightNo.setTextColor(mActivity.getColor(mSortState == 4 ? R.color.color_red : R.color.color_blue));
        mTvSender.setTextColor(mActivity.getColor(mSortState == 5 ? R.color.color_red : R.color.color_blue));

        onScanResult(mHeaderView.getSearchContent());
    }

    /**
     * 排序集合
     */
    private class SortComparator implements Comparator<LeavePortCollectCheckData> {

        private int type;

        public SortComparator(int type){
            this.type = type;
        }

        @Override
        public int compare(LeavePortCollectCheckData data1, LeavePortCollectCheckData data2) {
            switch (type){
                case 1 :
                    return data2.getNum() - data1.getNum();
                case 2 :
                    return ChinaToPinyin.getInstance().sort(data1.getTradeName() , data2.getTradeName());
                case 3 :
                    return ChinaToPinyin.getInstance().sort(data1.getDeap() , data2.getDeap());
                case 4 :
                    return ChinaToPinyin.getInstance().sort(data1.getFlightNo() , data2.getFlightNo());
                case 5 :
                    return ChinaToPinyin.getInstance().sort(data1.getSenderName() , data2.getSenderName());
                default:
                    return 0 ;
            }
        }
    }
}
